local que_zhanming = fk.CreateSkill {
    name = "que_zhanming",
    anim_type = "control",
}
Fk:loadTranslationTable {
    ["que_zhanming"] = "占命",
    [":que_zhanming"] = "<a href='fazeji_show'>共鸣技</a>，每轮开始时，你可以进行一次<a href='zhanbu_show'>占卜</a>，持有该技能的角色死亡时，清除所有占卜。",

    ["fazeji_show"] = "共鸣：锁定发动，无法被删除，无效或失效，除武将牌上拥有该技能的角色外，其他角色获得该技能后，无法发动此技能",

    ["zhanbu_show"] = "占卜：清除场上所有占卜效果，从效果池中随机出现X+1个占卜结果，依次选择X名其他角色施加占卜效果(X为你的体力值，至多为存活角色数)。",

    ["@[:]que_zhanming-round"] = "本轮占卜结果",

    ["@que_zhanming"] = "占命",
    ["#que_zhanming-invoke"] = "占命：请选择为 %dest 附加的命运",

    ["@@zm_loseskill-turn"] = "技能失效",

    ["zm_drwcard"] = "赐福",
    [":zm_drwcard"] = "准备阶段，随机摸1~3张牌",

    ["zm_discard"] = "摒弃",
    [":zm_discard"] = "准备阶段，随机弃1~3张牌",

    ["zm_hplose"] = "折夭",
    [":zm_hplose"] = "准备阶段，随机失去0~2体力",

    ["zm_hpre"] = "灵愈",
    [":zm_hpre"] = "准备阶段，随机回复0~2体力",

    ["zm_usecard"] = "先攻",
    [":zm_usecard"] = "准备阶段，有概率获得一张【杀】，并使用一张牌",

    ["zm_chai"] = "制约",
    [":zm_chai"] = "准备阶段，弃置一名其他角色随机1~2张牌",

    ["zm_light"] = "雷罚",
    [":zm_light"] = "准备阶段，进行判定，若为黑色，随机受到1~3点雷电伤害",

    ["zm_zhiheng"] = "守衡",
    [":zm_zhiheng"] = "准备阶段，重铸所有牌，若没有牌，改为随机摸1~3张牌",

    ["zm_toupai"] = "羽渡",
    [":zm_toupai"] = "准备阶段，获得一名角色随机1~2张牌",

    ["zm_tiaoguo"] = "轮回",
    [":zm_tiaoguo"] = "跳过下个判定和弃牌阶段",

    ["zm_fanmian"] = "修身",
    [":zm_fanmian"] = "结束阶段，摸1~3张牌并翻面",

    ["zm_jinsheng"] = "禁声",
    [":zm_jinsheng"] = "回合内，无法使用或弃置锦囊牌和基本牌",

    ["zm_qiangyun"] = "强运",
    [":zm_qiangyun"] = "立刻摸1~3张牌，本轮无法成为锦囊牌的目标。",

    ["zm_diyu"] = "低语",
    [":zm_diyu"] = "回合内，使用非基本牌时，摸两张牌，至多两次",

    ["zm_qiming"] = "启明",
    [":zm_qiming"] = "回合内，使用基本牌指定目标时，其无法响应",

    ["zm_jupi"] = "俱疲",
    [":zm_jupi"] = "回合内，仅可使用体力值等量张牌",

    ["zm_fuhuo"] = "福祸",
    [":zm_fuhuo"] = "回合内获得牌后，随机弃置一张牌(限三次)",

    ["zm_jihuo"] = "急火",
    [":zm_jihuo"] = "摸牌阶段多摸2~3张牌，本回合手牌上限减少等量",

    ["zm_xiusheng"] = "休生",
    [":zm_xiusheng"] = "手牌上限+1，准备阶段，回复2点体力，跳过出牌阶段",

    ["zm_cunmu"] = "寸目",
    [":zm_cunmu"] = "回合结束前，你的攻击距离固定为1",

    ["zm_loseskill"] = "遗落",
    [":zm_loseskill"] = "回合内，所有技能失效",

    ["zm_duohun"] = "堕魂",
    [":zm_duohun"] = "本轮无法使用【杀】和【酒】",

    ["zm_jieli"] = "解离",
    [":zm_jieli"] = "准备阶段，弃置所有装备牌",

    ["zm_chenmeng"] = "沉梦",
    [":zm_chenmeng"] = "跳过下个出牌和弃牌阶段",

    ["zm_fire"] = "焚身",
    [":zm_fire"] = "回合内，使用非基本牌时，受到一点无来源的火焰伤害",

    ["zm_woshiSB"] = "忏悔",
    [":zm_woshiSB"] = "准备阶段，视为对自己随机使用1~2张【杀】",

    ["zm_zhuoxin"] = "灼心",
    [":zm_zhuoxin"] = "本轮结束时，失去所有体力，若没有因此死亡，调整体力至本轮开始时，然后摸两张牌",
}

que_zhanming:addEffect(fk.RoundEnd, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_zhuoxin" and
            not player.dead
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local n = player:getMark("zhuoxinhp")
        room:notifySkillInvoked(player, "que_zhanming")
        room:loseHp(player, player.hp, que_zhanming.name)
        if not player.dead then
            if player.hp > n then
                room:changeHp(player, -math.abs(n - player.hp), nil, que_zhanming.name)
            elseif player.hp < n then
                room:changeHp(player, math.abs(n - player.hp), nil, que_zhanming.name)
            end
            player:drawCards(2, que_zhanming.name)
        end
        room:setPlayerMark(player, "@que_zhanming", 0)
    end,
})

que_zhanming:addEffect("atkrange", { --锁定攻击距离
    final_func = function(self, player)
        if player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_cunmu" then
            return 0
        end
    end
})

-- que_zhanming:addLoseEffect(function(self, player, is_death) --失去技能是获得
--     player.room:handleAddLoseSkills(player, "que_zhanming")
-- end)

que_zhanming:addEffect(fk.Death, { --死亡清理标记
    anim_type = "offensive",
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(que_zhanming.name, true, true)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        for _, i in ipairs(room.alive_players) do
            if i:getMark("@que_zhanming") ~= 0 then
                room:setPlayerMark(i, "@que_zhanming", 0)
            end
        end
    end,
})

que_zhanming:addEffect("prohibit", { --禁止使用牌，弃置牌，防止成为目标
    prohibit_use = function(self, player, card)
        if player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_jinsheng" then
            return card and card.type ~= Card.TypeEquip and player.phase ~= Player.NotActive
        end
        if player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_duohun" then
            return card and (card.name == "analeptic" or card.trueName == "slash")
        end
        if player:getMark("zm_jupi-turn") >= player.hp then
            return card
        end
        return false
    end,
    prohibit_discard = function(self, player, card) --无法弃置牌
        if player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_jinsheng" then
            return card and (card.type == Card.TypeBasic or
                card.type == Card.TypeTrick) and player.phase ~= Player.NotActive
        end
        return false
    end,
    is_prohibited = function(self, from, to, card) --不能成为牌的目标
        return (to:getMark("@que_zhanming") ~= 0 and to:getMark("@que_zhanming") == "zm_qiangyun") and card and
            card.type == Card.TypeTrick
    end,
})

que_zhanming:addEffect(fk.DrawNCards, { --增加摸牌量
    anim_type = "drawcard",
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("@que_zhanming") ~= 0 and
            player:getMark("@que_zhanming") == "zm_jihuo"
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local n = math.random(2, 3)
        data.n = data.n + n
        player.room:notifySkillInvoked(player, "que_zhanming")
        player.room:addPlayerMark(player, MarkEnum.MinusMaxCardsInTurn, n)
        player.room:setPlayerMark(player, "@que_zhanming", 0)
    end,
})

que_zhanming:addEffect(fk.AfterCardsMove, { --回合内获得牌时，弃牌
    anim_type = "drawcard",
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:getMark("@que_zhanming") ~= 0 and player:getMark("@que_zhanming") == "zm_fuhuo" and player == player.room.current then --回合内
            for _, move in ipairs(data) do
                if move.to == player and move.toArea == Player.Hand then
                    return true
                end
            end
        end
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player, "que_zhanming")
        room:throwCard(table.random(player:getCardIds("he"), 1), que_zhanming.name, player, player)
        room:addPlayerMark(player, "zm_fuhuo-turn")
        if player:getMark("zm_fuhuo-turn") > 3 then
            room:setPlayerMark(player, "@que_zhanming", 0)
        end
    end,
})

que_zhanming:addEffect(fk.CardUsing, { --使用牌触发技能
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("@que_zhanming") ~= 0 and
            (player:getMark("@que_zhanming") == "zm_diyu" or player:getMark("@que_zhanming") == "zm_qiming" or
                player:getMark("@que_zhanming") == "zm_jupi" or player:getMark("@que_zhanming") == "zm_fire") and
            data.card and player.phase ~= Player.NotActive
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player, "que_zhanming")
        if player:getMark("@que_zhanming") == "zm_diyu" and data.card.type ~= Card.TypeBasic then
            player:drawCards(2, que_zhanming.name)
            room:addPlayerMark(player, "zm_diyu_n-turn")
            if player:getMark("zm_diyu_n-turn") > 1 then
                room:setPlayerMark(player, "@que_zhanming", 0)
            end
        elseif player:getMark("@que_zhanming") == "zm_qiming" and data.card.type == Card.TypeBasic then
            data.disresponsiveList = table.simpleClone(room.players)
        elseif player:getMark("@que_zhanming") == "zm_jupi" then
            room:addPlayerMark(player, "zm_jupi-turn")
            if player:getMark("zm_jupi-turn") >= player.hp then
                room:setPlayerMark(player, "@que_zhanming", 0)
            end
        elseif player:getMark("@que_zhanming") == "zm_fire" then
            room:damage {
                to = player,
                damage = 1,
                damageType = fk.FireDamage,
                skillName = que_zhanming.name,
            }
            room:addPlayerMark(player, "zhanming_fire")
            if player:getMark("zhanming_fire") > 1 then
                room:setPlayerMark(player, "@que_zhanming", 0)
                room:setPlayerMark(player, "zhanming_fire", 0)
            end
        end
    end,
})


que_zhanming:addEffect(fk.EventPhaseChanging, { --跳过阶段
    can_refresh = function(self, event, target, player, data)
        return target == player and player:getMark("@que_zhanming") ~= 0 and
            (player:getMark("@que_zhanming") == "zm_tiaoguo" or player:getMark("@que_zhanming") == "zm_xiusheng" or player:getMark("@que_zhanming") == "zm_chenmeng") and
            table.contains(player:getTableMark("zm_tiaoguo_phase"), data.phase) and
            not data.skipped
    end,
    on_refresh = function(self, event, target, player, data)
        data.skipped = true
        player.room:removeTableMark(player, "zm_tiaoguo_phase", data.phase)
    end,
})

que_zhanming:addEffect(fk.EventPhaseStart, { --结束阶段发动的效果
    anim_type = "defensive",
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        local mark = player:getMark("@que_zhanming")
        local zb_list = { "zm_fanmian", "zm_jinsheng", "zm_tiaoguo", "zm_diyu", "zm_qiming", "zm_jupi", "zm_fuhuo",
            "zm_cunmu", "zm_jieli", "zm_loseskill", "zm_chenmeng", "zm_xiusheng" }
        return mark ~= 0 and table.contains(zb_list, mark) and target == player and player.phase == Player.Finish
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local skill = que_zhanming.name
        local mark = player:getMark("@que_zhanming")
        room:notifySkillInvoked(player, "que_zhanming")
        player:broadcastSkillInvoke("que_zhanming")
        if mark == "zm_fanmian" then
            player:turnOver()
            player:drawCards(math.random(1, 3), skill)
        end
        room:setPlayerMark(player, "@que_zhanming", 0)
    end
})

que_zhanming:addEffect(fk.EventPhaseStart, { --准备阶段发动的效果
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        local mark = player:getMark("@que_zhanming")
        local zb_list = { "zm_drwcard", "zm_discard",
            "zm_hplose", "zm_hpre",
            "zm_usecard", "zm_chai", "zm_light",
            "zm_zhiheng", "zm_toupai", "zm_xiusheng", "zm_loseskill", "zm_jieli", "zm_woshiSB" }
        return mark ~= 0 and table.contains(zb_list, mark) and target == player and player.phase == Player.Start
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local skill = que_zhanming.name
        room:notifySkillInvoked(player, "que_zhanming")
        local mark = player:getMark("@que_zhanming")
        if mark == "zm_woshiSB" then
            for i = 1, math.random(1, 2), 1 do
                room:useVirtualCard("slash", nil, player, player, que_zhanming.name)
            end
        end
        if mark == "zm_jieli" and #table.filter(player:getCardIds("he"), function(id)
                return Fk:getCardById(id).type == Card.TypeEquip
            end) > 0 then
            local cards = {}
            for _, i in ipairs(player:getCardIds("he")) do
                if Fk:getCardById(i).type == Card.TypeEquip then
                    table.insertIfNeed(cards, i)
                end
            end
            room:throwCard(cards, skill, player, player)
        end
        if mark == "zm_loseskill" then
            room:setPlayerMark(player, "@@zm_loseskill-turn", 1)
        end
        if mark == "zm_drwcard" then
            player:drawCards(math.random(1, 3), skill)
        end
        if mark == "zm_discard" and #player:getCardIds("he") > 0 then
            room:throwCard(table.random(player:getCardIds("he"), math.random(1, 3)), skill, player, player)
        end
        if mark == "zm_hplose" then
            room:loseHp(player, math.random(0, 2), skill)
        end
        if mark == "zm_hpre" and player:isWounded() then
            room:recover {
                who = player,
                num = math.random(0, 2),
                recoverBy = player,
                skillName = skill,
            }
        end
        if mark == "zm_xiusheng" and player:isWounded() then
            room:recover {
                who = player,
                num = 2,
                recoverBy = player,
                skillName = skill,
            }
        end
        if mark == "zm_usecard" then
            if 0.5 > math.random() then
                local card = room:getCardsFromPileByRule("slash", 1, "allPiles")
                if #card > 0 then
                    room:moveCards({
                        ids = card,
                        to = player,
                        toArea = Card.PlayerHand,
                        moveReason = fk.ReasonJustMove,
                        proposer = player,
                        skillName = skill,
                    })
                end
            end
            if #player:getCardIds("h") > 0 then
                room:askToPlayCard(player, {
                    skill_name = skill,
                    prompt = "占命：你可以使用一张牌，此牌不计次数且无距离限制",
                    cancelable = true,
                    extra_data = {
                        bypass_times = true,
                        bypass_distances = true,
                    },
                })
            end
        end
        if mark == "zm_chai" then
            local to = room:askToChoosePlayers(player, {
                targets = room:getOtherPlayers(player, false),
                min_num = 1,
                max_num = 1,
                prompt = "占命：请选择一名被弃置牌的其他角色",
                skill_name = skill,
                cancelable = true,
            })[1]
            if to then
                local x = math.min(#to:getCardIds("hej"), math.random(1, 2))
                local cards = room:askToChooseCards(player, {
                    target = to,
                    min = x,
                    max = x,
                    flag = "hej",
                    skill_name = skill,
                })
                if #cards > 0 then
                    room:throwCard(cards, skill, to, player)
                end
            end
        end
        if mark == "zm_light" then
            local judge = {
                who = target,
                reason = skill,
                pattern = ".|.|spade,club",
            }
            room:judge(judge)
            if judge.card.color == Card.Black then
                room:damage {
                    to = target,
                    damage = math.random(1, 3),
                    damageType = fk.ThunderDamage,
                    skillName = skill,
                }
            end
        end
        if mark == "zm_zhiheng" then
            if #player:getCardIds("hej") > 0 then
                room:recastCard(player:getCardIds("h"), player, skill)
            else
                player:drawCards(math.random(1, 3), skill)
            end
        end
        if mark == "zm_toupai" then
            local to = room:askToChoosePlayers(player, {
                targets = room:getOtherPlayers(player, false),
                min_num = 1,
                max_num = 1,
                prompt = "占命：请选择一名被偷牌的其他角色",
                skill_name = skill,
                cancelable = true,
            })[1]
            if to then
                local x = math.min(#to:getCardIds("hej"), math.random(1, 2))
                local cards = room:askToChooseCards(player, {
                    target = to,
                    min = x,
                    max = x,
                    flag = "hej",
                    skill_name = skill,
                })
                if #cards > 0 then
                    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, skill, nil, false, player)
                end
            end
        end
        if mark ~= "zm_xiusheng" then
            room:setPlayerMark(player, "@que_zhanming", 0)
        end
    end,
})

que_zhanming:addEffect(fk.RoundStart, {
    anim_type = "support",
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_zhanming.name, true, false) and
            (player.general == "que__kawei" or player.deputyGeneral == "que__kawei")
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player, "que_zhanming")
        player:broadcastSkillInvoke("que_zhanming")
        for _, i in ipairs(room.alive_players) do
            if i:getMark("@que_zhanming") ~= 0 then
                room:setPlayerMark(i, "@que_zhanming", 0)
            end
        end
        local zhanming_list = { "zm_drwcard", "zm_discard",
            "zm_hplose", "zm_hpre",
            "zm_usecard", "zm_chai", "zm_light",
            "zm_zhiheng", "zm_toupai", "zm_tiaoguo", "zm_fanmian",
            "zm_jinsheng", "zm_qiangyun", "zm_jupi", "zm_diyu", "zm_qiming", "zm_fuhuo", "zm_xiusheng", "zm_jihuo",
            "zm_cunmu", "zm_loseskill", "zm_duohun", "zm_jieli", "zm_chenmeng", "zm_fire", "zm_woshiSB", "zm_zhuoxin" }
        local ming = table.random(zhanming_list, math.min(#room.alive_players, player.hp) + 1)
        room:setBanner("@[:]que_zhanming-round", ming)
        if #ming > 0 then
            local n = #ming - 1
            for i = 1, #ming - 1, 1 do
                local targets = table.filter(room.alive_players, function(p)
                    return p:getMark("@que_zhanming") == 0
                end)
                local tos = room:askToChoosePlayers(player, {
                    min_num = 1,
                    max_num = 1,
                    targets = targets,
                    skill_name = que_zhanming.name,
                    prompt = "占命：请选择一名角色，为其赋予命运，共" ..
                        n .. "次，剩余" .. n - player:getMark("zhanming-phase") .. "次，点击左上角查看本轮效果池",
                    cancelable = false,
                })
                room:addPlayerMark(player, "zhanming-phase", 1)
                for _, id in ipairs(tos) do
                    local chs = room:askToChoice(player,
                        {
                            choices = ming,
                            prompt = "#que_zhanming-invoke::" .. id.id,
                            skill_name = que_zhanming.name,
                            detailed = true,
                        })
                    if chs == nil then chs = table.random(ming) end
                    room:setPlayerMark(id, "@que_zhanming", chs)
                    if chs == "zm_zhuoxin" then
                        room:setPlayerMark(id, "zhuoxinhp", id.hp)
                    end
                    if chs == "zm_tiaoguo" then
                        room:setPlayerMark(id, "zm_tiaoguo_phase", { Player.Judge, Player.Discard })
                    end
                    if chs == "zm_chenmeng" then
                        room:setPlayerMark(id, "zm_tiaoguo_phase", { Player.Play, Player.Discard })
                    end
                    if chs == "zm_qiangyun" then
                        id:drawCards(math.random(1, 3), que_zhanming.name)
                    end
                    if chs == "zm_xiusheng" then
                        room:addPlayerMark(player, MarkEnum.AddMaxCards, 1)
                        room:setPlayerMark(id, "zm_tiaoguo_phase", { Player.Play })
                    end
                    table.removeOne(ming, chs)
                end
            end
        end
    end,
})

que_zhanming:addEffect("invalidity", {
    invalidity_func = function(self, from, skill)
        return from:getMark("@@zm_loseskill-turn") > 0 and skill:isPlayerSkill(from) --技能失效，没什么好讲的
    end,
})
return que_zhanming
